<?php

namespace App\Models\Factory;

use App\Helpers\Generator\TokenGenerator;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\Collection;
use App\Models\Orm\Feedback;
use App\Models\Orm\Product;
use App\Models\Orm\User;
use App\Models\Orm\UserBanks;
use App\Models\Orm\UserCertify;
use App\Models\Orm\UserIdentity;
use App\Models\Orm\UserProfile;
use App\Strategies\SexStrategy;

/**
 * 收藏产品
 * Class FeedbackFactory
 * @package App\Models\Factory
 */
class CollectionFactory extends AbsModelFactory
{


    /**
     * 添加收藏
     */
    public static function addCollection($data)
    {
        $collection = Collection::insert($data);
        return $collection;
    }

    /**
     * 查询用户收藏的产品
     */
    public static function getCollection($data,$productId){
        $collection = Collection::where(['uid' => $data,'product_id' => $productId])->first();
        return $collection;
    }

    /**
     * 取消收藏
     */
    public static function deleteCollection($data,$productId){
        $collection = Collection::where(['uid' => $data,'product_id' => $productId])->delete();
        return $collection;
    }
    /**
     * 根据用户id查找产品id
     */
    public static function getProductByuserId($data){
        $productId = Collection::select('product_id')->where(['uid' => $data])->get();
        return $productId;

    }
    /**
     * 根据产品id数组查询出所有该产品
     */
    public static function getListByProductId($data,$uid,$newId){
        $product = Product::with('labels')
            ->select(['product.pname','product.id','product.product_introduction','product.product_logo','product.min_algorithm','fastest_time','product.interest_algorithm','product.activity','product.create_time','product.minimum_amount','product.maximum_amount'])
            ->join('collection', 'product.id', '=', 'collection.product_id')
            ->whereIn('product.id',$data)
            ->where('uid','=',$uid)
            ->where('product.status',1)
            ->orderby('collection.id','desc')
            ->get();
        //循环判断一下产品是否是新品
        foreach ( $product as $k =>$v)
        {
           if($v -> id < $newId)
           {
               $product[$k]->online = 1;
           }else{
               $product[$k]->online = 2;

           }
        }

        return $product;

    }


}